package com.iMusic.businessLogicLayer;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

import com.iMusic.integrationLayer.DBConnection;
import com.iMusic.integrationLayer.DBConnectionManager;

public class TaxManager {

	HashMap<String, Float> taxes = null;
	static private TaxManager taxManager = null;
	
	/*
	 * Constructor
	 */
	private TaxManager() throws SQLException {
		if (taxes == null) {
			taxes = new HashMap<String, Float>();
			loadTaxes();
			
			
		}
	}
	
	/*
	 * Load taxes from Database
	 */
	private void loadTaxes() throws SQLException {
		
		DBConnectionManager dbConnMgr = DBConnectionManager.getDBConnectionManager();
		DBConnection dbConn = dbConnMgr.getDBConnection();
		
		dbConn.connect();
		
		ResultSet rs = dbConn.query("SELECT PROVINCE, PERCENTAGE " +
									"FROM TAX ");
		
		if (taxes == null) {
			taxes = new HashMap<String, Float>();
		}

		while (rs != null && rs.next()) {
			taxes.put(rs.getString("PROVINCE"), rs.getFloat("PERCENTAGE"))	;
		}
		
		dbConn.disconnect();
		dbConnMgr.freeDBConnection(dbConn);
	}
	
	/*
	 * Returns TaxManager instance
	 */
	static public TaxManager getInstance() {
		if (taxManager == null) {
			try {
				taxManager = new TaxManager();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return taxManager;
	}
	
	/*
	 * Calculate federal taxes
	 */
	public float getFederalTax(float amount) {
		float tax = 0;
		
		float federalPercentage = taxes.get("##");
		
		tax = (float) (Math.floor(amount * federalPercentage/100 * 100) / 100);
		
		return tax;
	}
	
	
	/*
	 * Calculate provincial taxes
	 */
	public float getProvincialTax (String province, float amount) {
		float tax = 0;
		float percentage = 0;

		float federalPercentage = taxes.get("##");
		try {
			percentage = taxes.get(province);
		} catch (Exception e) {
			percentage = 0;
		}
		
		float federalTax = (float) (Math.floor(amount * federalPercentage/100 * 100) / 100);
		
		tax = (float) (Math.floor((amount + federalTax) * percentage/100 * 100) / 100);
		
		return tax;
	}
	
	
	public int getFederalTaxType() throws SQLException {
		
		int ret = -1;
		
		DBConnectionManager dbConnMgr = DBConnectionManager.getDBConnectionManager();
		DBConnection dbConn = dbConnMgr.getDBConnection();
		
		dbConn.connect();
		
		ResultSet rs = dbConn.query("SELECT TYPE " +
									"FROM TAX " +
									"WHERE Province = '##'");
		
		if (rs != null && rs.next()) {
			ret = rs.getInt("TYPE");
		}
		
		dbConn.disconnect();
		dbConnMgr.freeDBConnection(dbConn);
		
		return ret;
	}
	

	public int getProvincialTaxType(String province) throws SQLException {
		
		int ret = -1;
		
		DBConnectionManager dbConnMgr = DBConnectionManager.getDBConnectionManager();
		DBConnection dbConn = dbConnMgr.getDBConnection();
		
		dbConn.connect();
		
		ResultSet rs = dbConn.query("SELECT TYPE " +
									"FROM TAX " +
									"WHERE Province = '" + province + "'");
		
		if (rs != null && rs.next()) {
			ret = rs.getInt("TYPE");
		}
		
		dbConn.disconnect();
		dbConnMgr.freeDBConnection(dbConn);
		
		return ret;
	}

	
}
